package com.xoa.service.easyReport.impl;

import com.xoa.dao.easyReport.RepDataMapper;
import com.xoa.dao.easyReport.RepFieldMapper;
import com.xoa.dao.easyReport.RepFlowMapper;
import com.xoa.dao.easyReport.RepTableMapper;
import com.xoa.model.easyReport.RepData;
import com.xoa.model.easyReport.RepField;
import com.xoa.model.easyReport.RepFlow;
import com.xoa.model.easyReport.RepTable;
import com.xoa.model.users.Users;
import com.xoa.service.easyReport.RepDataService;
import com.xoa.service.easyReport.RepFieldService;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepDataServiceImpl implements RepDataService {

    @Resource
    RepTableMapper repTableMapper;

    @Resource
    RepFieldMapper repFieldMapper;

    @Resource
    RepFlowMapper repFlowMapper;

    @Resource
    RepDataMapper repDataMapper;

    @Autowired
    RepFieldService repFieldService;

    @Override
    public ToJson<RepTable> inserRepDatas(HttpServletRequest request, RepTable repTable) {
        Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        ToJson<RepTable> toJson = new ToJson<>(1,"err");
        try {
            RepTable repTable1 = repTableMapper.selectByPrimaryKey(repTable.getRepTableId());
            if(repTable1 != null && "1".equals(repTable1.getStatus())){
                RepFlow repFlow = new RepFlow();
                repFlow.setRepTableId(repTable.getRepTableId());
                repFlow.setFlowTime(new Date());
                repFlow.setUserId(user.getUserId());
                int i = repFlowMapper.insertSelective(repFlow);
                if(i>0){
                    List<RepData> list = repTable.getRepDataList();
                    if(list != null ){
                        for(RepData repData :list){
                            repData.setFlowId(repFlow.getFlowId());
                            repDataMapper.insertSelective(repData);
                        }
                        toJson.setFlag(0);
                        toJson.setMsg("true");
                    }
                }
            }else{
                toJson.setMsg("报表未就绪");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 创建人: liujian
     * 创建时间: 2020/2/6
     * 描述: 修改已填报的数据
     * 参数: [request, repTable]
     * 返回值: com.xoa.util.ToJson<com.xoa.model.easyReport.RepTable>
     */
    @Override
    public ToJson<RepFlow> updateRepDatas(HttpServletRequest request, RepFlow repFlow) {
        ToJson<RepFlow> toJson = new ToJson<>(1,"err");
        try {
            RepTable repTable = repTableMapper.selectByPrimaryKey(repFlow.getRepTableId());
            if("1".equals(repTable.getStatus())){
                List<RepData> repDataList = repFlow.getRepDataList();
                if(repDataList != null){
                    for (RepData repData : repDataList) {
                        repData.setFlowId(repFlow.getFlowId());
                        //如果之前无填报数据，需要新增一条
                        if(repDataMapper.selectByFlowField(repFlow.getFlowId(),repData.getFieldId()) != null){
                            repDataMapper.updateByPrimaryKeySelective(repData);
                        }else{
                            repDataMapper.insertSelective(repData);
                        }
                    }
                    toJson.setFlag(0);
                    toJson.setMsg("true");
                }
            }else{
                toJson.setFlag(0);
                toJson.setMsg("填报以结束或未就绪");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<RepFlow> delRepDataByFlowId(HttpServletRequest request,Integer tableId, Integer flowId) {
        ToJson<RepFlow> toJson = new ToJson<>(1,"err");
        try {
            RepTable repTable = repTableMapper.selectByPrimaryKey(tableId);
            if("1".equals(repTable.getStatus())){
                int i = repFlowMapper.deleteByPrimaryKey(flowId);
                if(i>0){
                    repDataMapper.deleteByFlowId(flowId);
                    toJson.setFlag(0);
                    toJson.setMsg("true");
                }else{
                    toJson.setMsg("删除失败");
                }
            }else{
                toJson.setFlag(0);
                toJson.setMsg("填报已结束");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<RepFlow> findRepDataByFlowId(HttpServletRequest request, Integer flowId) {
        ToJson<RepFlow> toJson = new ToJson<>(1,"err");
        try {
            RepFlow repFlow = repFlowMapper.selectByPrimaryKey(flowId);
            List<RepField> repFieldList= repFieldService.recursionField(repFlow.getRepTableId());
            RepData repData =new RepData();
            repData.setFlowId(repFlow.getFlowId());
            List<RepData> list = repDataMapper.selectCondition(repData);
            repFlow.setRepDataList(list);
            repFlow.setRepFieldList(repFieldList);
            toJson.setObject(repFlow);
            toJson.setFlag(0);
            toJson.setMsg("true");
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson<RepFlow> findRepDatas(HttpServletRequest request,Integer page,Integer limit,boolean useFlag, String type, Integer tableId,String startTime , String endTime) {
        ToJson<RepFlow> toJson = new ToJson<>(1,"err");
        Map map = new HashMap();
        try {
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(useFlag);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());

            String userId = null;
            if("1".equals(type)){
                userId = user.getUserId();
            }
            RepTable repTable = repTableMapper.selectByPrimaryKey(tableId);
            if(repTable != null && "1".equals(repTable.getStatus())){
                //截止时间
                Date closingTime = repTable.getClosingTime();
                Date date = new Date();

                map.put("tableId",tableId);
                map.put("userId",userId);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<RepFlow> repFlows = repFlowMapper.selectByTableIdTimeMap(map);
                if(repFlows != null){
                    List<RepField> repFieldList= repFieldMapper.selectByTableId(tableId);
                    for(RepFlow repFlow1 : repFlows){
                        RepData repData =new RepData();
                        repData.setFlowId(repFlow1.getFlowId());
                        List<RepData> list = repDataMapper.selectCondition(repData);
                        repFlow1.setRepDataList(list);
                        repFlow1.setRepFieldList(repFieldList);

                        //判断截止时间   编辑 和 删除  只能 在截止时间内显示  ，  如果截止时间没有  一直显示
                        if (closingTime != null){
                            if ( date.getTime() < closingTime.getTime() ){
                                repFlow1.setPrivlook(true);
                            }else{
                                repFlow1.setPrivlook(false);
                            }
                        }else{
                            repFlow1.setPrivlook(true);
                        }
                    }
                    toJson.setObj(repFlows);
                    PageParams pageParams = (PageParams) map.get("pageParams");
                    toJson.setTotleNum(pageParams.getTotal());
                    toJson.setFlag(0);
                    toJson.setMsg("true");
                }else{
                    toJson.setFlag(0);
                    toJson.setMsg("未填报");
                }
            }else{
                toJson.setMsg("报表未就绪");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
